package com.wenhuang.chatgptopen.controller;


import com.wenhuang.chatgptopen.pojo.Topic;
import com.wenhuang.chatgptopen.retu.TopicReturn;
import com.wenhuang.chatgptopen.service.TopicService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;

/*
 *@Time：2023/6/3
 *@Author：Jelly
 */
@Controller
public class TopicController {

    @Autowired
    private TopicService topicService;
    private TopicReturn topicReturn;

    //发帖功能
    @PostMapping("/addTopic")
    @ResponseBody
    public TopicReturn addTopic(Topic topic) {

        //获取时间
        Date date = new Date();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String time = dateFormat.format(date);
        topic.setTime(time);

        topicReturn = new TopicReturn();
        String result = "";
        if ((topicService.addContent(topic)) != null) {
            result = "success";
        } else {
            result = "fail";
        }
        topicReturn.setResult(result);
        return topicReturn;
    }

    @PostMapping("/getTopic")
    @ResponseBody
    public TopicReturn getTopic(Topic topic) {

        topicReturn = new TopicReturn();
        if (topic.getTitle() == null || "".equals(topic.getTitle())) {
            topicReturn.setResult("fail");
        } else {
            Topic topic1 = topicService.selectTopicByTitleAndUserId(topic);
            topicReturn.setTitle(topic1.getTitle());
            topicReturn.setContent(topic1.getContent());
            topicReturn.setTime(topic1.getTime());
            topicReturn.setResult("success");
        }
        return topicReturn;
    }

    //删帖功能
    @PostMapping("/deleteTopic")
    @ResponseBody
    public TopicReturn deleteTopic(Topic topic) {


        topicReturn = new TopicReturn();
        String result = "";
        if ((topicService.deleteTopic(topic)) != null) {
            result = "success";
        } else {
            result = "fail";
        }
        topicReturn.setResult(result);
        return topicReturn;
    }

}
